from django.db import models
from datetime import datetime
# Create your models here.

# 职级表
class RankModel(models.Model):
    name = models.CharField('职级名称', max_length=28)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta:
        db_table = 'rank_tb'
        verbose_name = '职级表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 部门表
class SectionModel(models.Model):
    name = models.CharField('部门名称', max_length=28)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta:
        db_table = 'section_tb'
        verbose_name = '部门表'
        verbose_name_plural = verbose_name

# 职位表
class StationModel(models.Model):
    name = models.CharField('职位名称', max_length=28)
    ranks = models.ForeignKey(RankModel,on_delete=models.CASCADE, verbose_name='所属职级')
    sections = models.ForeignKey(SectionModel,on_delete=models.CASCADE, verbose_name='所属部门')
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta:
        db_table = 'station_tb'
        verbose_name = '职级表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 角色表
class RoleModel(models.Model):
    name = models.CharField('角色名称', max_length=256)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta:
        db_table = 'role_tb'
        verbose_name = '角色表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 权限表
class PermissionModel(models.Model):
    name = models.CharField('权限名称', max_length=100)
    path = models.CharField('权限', max_length=256)
    roles = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name='所属角色')
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta:
        db_table = 'permission_tb'
        verbose_name = '权限表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

#
#目录表
class Catalog(models.Model):
    catalog_name = models.CharField(max_length=20,null=True,blank=True,verbose_name='目录名称')
    catalog_path = models.CharField(max_length=256, null=True, blank=True, verbose_name='目录路径')
    catalog_icon = models.CharField(max_length=256, null=True, blank=True, verbose_name='目录图标')
    catalog_remark = models.CharField(max_length=30, null=True, blank=True, verbose_name='目录备注')
    is_delete = models.BooleanField(default=False, verbose_name="是否删除")
    catalog_sort = models.IntegerField(null=True,blank=True,verbose_name='排序号')
    class Meta:
        db_table ='catalog_tb'
        verbose_name = '目录表'
        verbose_name_plural = verbose_name
    def __str__(self):
        return '目录名称:%s'%self.catalog_name

# 目录 菜单表
class Menu(models.Model):
    menu_name = models.CharField(max_length=20,null=True,blank=True,verbose_name='菜单名称')
    menu_path = models.CharField(max_length=256,null=True,blank=True,verbose_name='菜单路径')
    menu_icon = models.CharField(max_length=256,null=True,blank=True,verbose_name='菜单图标')
    menu_remark = models.CharField(max_length=30,null=True,blank=True,verbose_name='菜单备注')
    menu_sort = models.IntegerField(null=True,blank=True,verbose_name='排序号')
    catalog = models.ForeignKey(Catalog,on_delete=models.CASCADE,verbose_name='所属目录')
    class Meta:
        db_table ='menu_tb'
        verbose_name = '目录-菜单表'
        verbose_name_plural = verbose_name
    def __str__(self):
        return '菜单名称:%s'%self.menu_name

# 角色 目录 权限表
class Role_Catalog(models.Model):
    role = models.ForeignKey(RoleModel,on_delete=models.CASCADE,verbose_name='角色')
    catalog = models.ForeignKey(Catalog,on_delete=models.CASCADE,verbose_name='目录')
    is_catalog = models.BooleanField(null=True,blank=True,verbose_name='是否有某个目录权限')
    class Meta:
        db_table ='alog_tb'
        verbose_name = '角色目录表'
        verbose_name_plural = verbose_name
    def __str__(self):
        return f'角色id:{self.role_id},目录id:{self.catalog_id},是否有这个目录权限{self.catalog_id}'

# 角色 菜单表
class Role_Menu(models.Model):
    role = models.ForeignKey(RoleModel,on_delete=models.CASCADE,verbose_name='角色')
    menu = models.ForeignKey(Menu,on_delete=models.CASCADE,verbose_name='所属菜单')
    is_menu = models.BooleanField(null=True,blank=True,verbose_name='是否有某个菜单权限')
    catalog = models.ForeignKey(Catalog, on_delete=models.CASCADE, verbose_name='所属目录')
    class Meta:
        db_table ='pole_catalog_tb'
        verbose_name = '角色菜单表'
        verbose_name_plural = verbose_name
    def __str__(self):
        return f'角色id:{self.role_id},菜单id:{self.menu_id},是否有这个菜单权限{self.is_menu}'
#

# 职位角色中间表
class StationRoleModel(models.Model):
    station = models.ForeignKey(StationModel, on_delete=models.CASCADE, verbose_name='职位')
    role = models.ForeignKey(RoleModel, on_delete=models.CASCADE, verbose_name='所属角色')

    class Meta:
        db_table = 'station_role_tb'
        verbose_name = '职位角色表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s 拥有 %s角色'%(self.station.name,self.role.name)

# 员工表
class UserModel(models.Model):

    STATUS_TYPE = (
        (0, '待入职'),
        (1, '实习生'),
        (2, '正式员工'),
        (3, '待离职'),
        (4, '待转正'),
    )

    SEX = (
        (1, '男'),
        (2, '女')
    )

    name = models.CharField('员工姓名', max_length=20)
    mobile = models.CharField('手机号',max_length=11,null=True,blank=True, unique=True)
    password = models.CharField('密码', max_length=256)
    account = models.CharField('账号', max_length=28, unique=True)
    sex = models.SmallIntegerField('性别', choices=SEX, default=1)
    age = models.IntegerField('年龄')
    birthday = models.DateField('生日')
    email = models.CharField('邮箱', max_length=50, unique=True)
    is_state = models.SmallIntegerField('状态', choices=STATUS_TYPE, default=1)
    stations = models.ForeignKey(StationModel, on_delete=models.CASCADE, verbose_name='所属职位')
    address = models.CharField(max_length=256, null=True, blank=True, verbose_name='户籍')
    profile_photo = models.CharField(max_length=800, null=True, blank=True, verbose_name='头像')
    is_delete = models.BooleanField(default=False, verbose_name="是否删除")
    entry_time = models.DateField(default=datetime.now(), verbose_name='入职时间')
    positive_time = models.DateField(null=True, blank=True, verbose_name='转正时间')
    dimission_time = models.DateField(null=True, blank=True, verbose_name='离职时间')
    role = models.ForeignKey(RoleModel, null=True, blank=True, on_delete=models.CASCADE, verbose_name='所属角色')
    department = models.ForeignKey(SectionModel, null=True, blank=True, on_delete=models.CASCADE, verbose_name='所属部门')
    class Meta:
        db_table = 'user_tb'
        verbose_name = '员工表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 流程表
class FlowModel(models.Model):
    name = models.CharField('流程名称', max_length=50)
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    people = models.CharField('适用人群', max_length=20, default='全部')
    approval = models.CharField('审批层级', max_length=10)
    # users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='所属员工')

    class Meta:
        db_table = 'flow_tb'
        verbose_name = '流程表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 流程节点表
class NodeModel(models.Model):

    STATUS_TYPE = (
        (0, '未审批'),
        (1, '已同意'),
        (2, '已驳回'),
    )

    name = models.CharField('节点名', max_length=50)
    flows = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name='所属流程')
    sequence = models.IntegerField(verbose_name='顺序')
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    is_status = models.SmallIntegerField('状态', choices=STATUS_TYPE, default=0)

    class Meta:
        db_table = 'node_tb'
        verbose_name = '流程节点表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 员工流程实例表  历史表
class InstanceModel(models.Model):
    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='所属员工')
    flows = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name='所属流程')
    create_time = models.DateTimeField('发起时间', auto_now_add=True)
    complete_time = models.CharField('完成时间', max_length=50)

    class Meta:
        db_table = 'instance_tb'
        verbose_name = '员工流程实例表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s 下的流程实例信息'%(self.users.name)

# 审批表
class ApprovalModel(models.Model):

    STATUS_TYPE = (
        (0, '未审批'),
        (1, '已同意'),
        (2, '已驳回'),
    )

    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='审批人')
    nodes = models.ForeignKey(NodeModel, on_delete=models.CASCADE, verbose_name='所属节点')
    is_status = models.SmallIntegerField('状态', choices=STATUS_TYPE, default=0)
    operate_time = models.DateTimeField('操作时间', auto_now=True)
    # instances = models.ForeignKey(InstanceModel, on_delete=models.CASCADE, verbose_name='所属流程实例')
    content = models.CharField('备注', max_length=128)

    class Meta:
        db_table = 'approval_tb'
        verbose_name = '审批表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s %s 你的需求'%(self.users.name,self.is_status)

# 节点信息表
class InodeModel(models.Model):
    content = models.CharField('描述', max_length=128)
    flows = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name='所属流程')
    nodes = models.ForeignKey(NodeModel, on_delete=models.CASCADE, verbose_name='所属节点')

    class Meta:
        db_table = 'inode_tb'
        verbose_name = '节点信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s 中 %s节点的信息'%(self.flows.name,self.nodes.name)

# 员工流程信息表
class FlowinfoModel(models.Model):
    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='所属员工')
    flows = models.ForeignKey(FlowModel, on_delete=models.CASCADE, verbose_name='所属流程')
    inode = models.ForeignKey(InodeModel, on_delete=models.CASCADE, verbose_name='所属节点信息')
    content = models.CharField('内容', max_length=128)

    class Meta:
        db_table = 'flowinfo_tb'
        verbose_name = '员工流程信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s 中 %s流程下的信息'%(self.users.name,self.flows.name)

# 操作日志
class HandleModel(models.Model):
    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='操作人')
    users_IP = models.CharField('登录ip地址', max_length=100, null=True)
    login_device = models.CharField('登录设备', max_length=256, null=True)
    handle_url = models.CharField('操作路径', max_length=100)
    handle_action = models.CharField('操作动作', max_length=20)
    handle_step = models.CharField('操作描述', max_length=100)
    handle_time = models.DateTimeField('操作日期', auto_now=True)

    class Meta:
        db_table = 'handle_tb'
        verbose_name = '操作日志表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s 在 %s 操作了 %s'%(self.users.name,self.handle_time,self.handle_url)

# # 登录日志
# class LoginModel(models.Model):
#     users_IP = models.CharField('登录ip地址', max_length=100)
#     login_time = models.DateTimeField('登录时间', auto_now_add=True)
#     login_device = models.CharField('登录设备', max_length=30)
#     login_site = models.CharField('登录地点', max_length=100)
#     users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='员工登录id')
#
#     class Meta:
#         db_table = 'login_tb'
#         verbose_name = '登录日志表'
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return '%s 在 %s 用 %s 登录'%(self.users.name,self.login_time,self.login_device)

# 材料管理
class MaterialsModel(models.Model):
    name = models.CharField(verbose_name="名称", max_length=20)
    url = models.CharField(verbose_name="地址", max_length=256)
    create_date = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table="materials_tb"
        verbose_name = "材料表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

# 工资条管理
class PriceModel(models.Model):
    users = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='所属员工')
    attendance_day = models.IntegerField(verbose_name="出勤天数")
    miner_hour = models.IntegerField(verbose_name="旷工时间")
    leave_day = models.IntegerField(verbose_name="请假天数")
    base_price = models.DecimalField(verbose_name="基本工资", max_digits=9,decimal_places=2)
    position_price = models.DecimalField(verbose_name="岗位工资", max_digits=9,decimal_places=2)
    create_date = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)
    class Meta():
        db_table="price_tb"
        verbose_name = "工资条表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.users
